from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.devtools.v136.fed_cm import click_dialog_button
from selenium.webdriver.support.expected_conditions import element_to_be_clickable
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from urllib.parse import unquote, urlparse, parse_qs
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
import requests
import os
import time
import random


#无分页,滚动刷新的网页爬虫
os.makedirs('bing_photos_5', exist_ok=True)

# 设置 Edge 浏览器选项
edge_options = Options()
edge_options.add_argument('--headless')  # 无头模式
edge_options.add_argument('--disable-gpu')
edge_options.add_argument('--no-sandbox')
edge_options.add_argument('--disable-dev-shm-usage')
edge_options.add_argument('--start-maximized')  # 最大化窗口确保所有元素可见
edge_options.add_argument('--log-level=3')  # 减少日志输出

# 初始化 Edge WebDriver
driver = webdriver.Edge()

# 从原始URL解析实际图片地址
def extract_image_url(original_url):
    parsed_url = urlparse(original_url)
    query_params = parse_qs(parsed_url.query)
    objurl = query_params.get('mediaurl', [''])[0]
    return unquote(objurl)  # 解码URL编码


# 下载图片
def download_image(url, save_path):
    response = requests.get(url, stream=True)
    if response.status_code == 200:
        with open(save_path, 'wb') as f:
            for chunk in response.iter_content(1024):
                f.write(chunk)
        print(f"图片已保存至: {save_path}")
    else:
        print("下载失败，状态码:", response.status_code)

try:
    # 设置请求头
    driver.execute_cdp_cmd('Network.setUserAgentOverride', {
        "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.59"
    })

    print("正在访问 baidu...")

    # 打开目标网页
    driver.get("https://cn.bing.com/images/search?q=%E7%A9%BA%E5%AE%A4%E5%86%85%E5%9C%BA%E6%99%AF%E5%9B%BE&qs=n&form=QBIR&sp=-1&lq=0&pq=kong%E5%AE%A4%E5%86%85%E5%9C%BA%E6%99%AF%E5%9B%BE&sc=8-9&cvid=45A0E78EEBB945C99AE5C65203D9144A&first=1&cw=2543&ch=1305")
    time.sleep(3)

    # 模拟滚动加载更多图片
    last_height = driver.execute_script("return document.body.scrollHeight")
    scroll_attempts = 0
    print("开始滚动加载图片...")

    while scroll_attempts < 200:  # 最多滚动200次
        # 滚动到页面底部
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

        # 随机等待时间（更人性化）
        time.sleep(random.uniform(1.5, 3.5))

        # 检查页面高度是否变化
        new_height = driver.execute_script("return document.body.scrollHeight")
        if new_height == last_height:
            break
        last_height = new_height
        scroll_attempts += 1
        print(f"已滚动 {scroll_attempts} 次，当前高度: {new_height}px")

    image_containers = driver.find_elements(By.CSS_SELECTOR, 'div[class="imgpt"]')
    print(f"找到 {len(image_containers)} 张图片")

    for idx, container in enumerate(image_containers):
        try:
            img_element = container.find_element(By.CSS_SELECTOR, 'a')

            img_source_url = img_element.get_attribute('href')
            img_url = extract_image_url(img_source_url)
            print("解析出的图片地址:", img_url)

            save_path = f"bing_photos_5/empty_room_{idx + 1}.jpg"
            download_image(img_url, save_path)
        except Exception as e:
            print(f"下载第 {idx + 1} 张图片时出错: {str(e)}")



finally:
    # 关闭浏览器
    driver.quit()
    print("Edge 浏览器已关闭，爬取完成！")